package yl.hs.bmipfull.controllers.production;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.production.IProductionLogService;
import yl.hs.bmipfull.service.system.IUserService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.stream.Collectors;

/*
 * ProductionLogController
 * @category
 * @author 谢景广
 * @date 2021-06-17
 */
@Api(value = "生产操作日志Web接口", tags = "生产管理——生产日志信息")
@RequestMapping(value = "/api/production/log")
@RestController
public class ProductionLogController extends ControllerBase {

    @Autowired
    IProductionLogService logSvc;

    @Autowired
    IUserService userSvc;

    @ApiOperation(value = "查询销售操作日志", notes = "根据销售操作日志编号查询销售操作日志")
    @RequestMapping(value = "/{dataSn}", method = RequestMethod.GET)
    public WebResult queryLogBySn(@ApiParam(value = "数据编号") @PathVariable String dataSn) {
        try {
            var list = logSvc.queryByDataSn(dataSn);
            var userSNList = list.stream().map(t -> t.getUserSN()).collect(Collectors.toList());
            var userList = userSvc.queryByUserSNList(userSNList);
            var newList = list.stream().map(t -> {
                var item = t.toMap();
                var user = userList.stream().filter(p -> t.getUserSN().equals(p.getSn())).collect(Collectors.toList());
                var userName = "";
                if (user.size() > 0) {
                    userName = user.get(0).getRealname();
                }
                item.put("userName", userName);
                return item;
            }).collect(Collectors.toList());
            return success("查询成功", newList);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询销售操作日志", notes = "根据销售操作日志编号查询销售操作日志")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryLogBy(@RequestBody PageQuery query,
                                @ApiParam(value = "页码") @PathVariable int page,
                                @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        var data = logSvc.queryCP(query, rowNum, page);
        return success("查询成功",data);

    }
}
